package com.keti.DaoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.keti.Dao.stuffDao;
import com.keti.pojo.User;
import com.keti.pojo.stuff;

import util.DBUtil;

public class suffDaoImpl implements stuffDao {

	Connection conn = null;
	PreparedStatement ps =null;
	ResultSet rs = null;
	@Override
	public void add(stuff s) {
		conn=DBUtil.getconn();
		String sql="INSERT `stuff`(sname,count1,count2,status) VALUE(?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			String[] params={
					s.getSname(),
					String.valueOf(s.getCount1()),
					String.valueOf(s.getCount2()),
					String.valueOf(s.getStatus())
					
				};
				DBUtil.exeUpd(sql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void updata(stuff s) {
		conn = DBUtil.getconn();
		String sql="UPDATE `stuff` set sname=?,count1=?,count2=?,status=? where id=?";
		try {
			ps = conn.prepareStatement(sql);
			String[] params= {
					s.getSname(),
					String.valueOf(s.getCount1()),
					String.valueOf(s.getCount2()),
					s.getStatus(),
					String.valueOf(s.getId())
			};
			DBUtil.exeUpd(sql, params);
		
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void del(int id) {
		conn = DBUtil.getconn();
		try {
			ps = conn.prepareStatement("DELETE from `stuff` where id=?");
			ps.setInt(1, id);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public List<stuff> queryAll() {
		List<stuff> list = new ArrayList<stuff>();
		conn = DBUtil.getconn();
		try {
			ps = conn.prepareStatement("select *from `stuff`");
			rs = ps.executeQuery();
			stuff s =null;
			while(rs.next()) {
				int id = rs.getInt(1);
				String sname = rs.getString(2);
				int count1 = rs.getInt(3);
				int count2 = rs.getInt(4);
				String status = rs.getString(5);
				s = new stuff();
				s.setId(id);
				s.setSname(sname);
				s.setCount1(count1);
				s.setCount2(count2);
				s.setStatus(status);
				list.add(s);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public stuff queryOne(int id) {
		conn = DBUtil.getconn();
		stuff s = new stuff();
		try {
			String sql = "SELECT *from `stuff` where id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs = ps.executeQuery();
			while(rs.next()) {
				id = rs.getInt("id");
				String sname = rs.getString("sname");
				int count1 = rs.getInt("count1");
				int count2 = rs.getInt("count2");
				String status = rs.getString("status");
				s.setId(id);
				s.setSname(sname);
				s.setCount1(count1);
				s.setCount2(count2);
				s.setStatus(status);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				DBUtil.closeAll(conn, ps, rs);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return s;
	}

}
